﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using TowerDef_s_enseLibrary.game;
using TowerDef_s_enseLibrary.enemy;

namespace TowerDef_s_enseLibrary.tower
{
    public class Tower : Sprite
    {
        #region variables

        protected int cost;
        /// <summary>
        /// The cost of a tower
        /// </summary>
        public int Cost
        {
            get { return cost; }
        }
        protected int damage;
        /// <summary>
        /// Get the damage done to _enemies
        /// </summary>
        public int Damage
        {
            get { return damage; }
        }

        protected float bulletTimer;
        protected float radius;
        /// <summary>
        /// Get how far the tower can shoot
        /// </summary>
        public float Radius
        {
            get { return radius; }
        }

        protected Enemy target;
        /// <summary>
        /// get the current Target
        /// </summary>
        public Enemy Target
        {
            get { return target; }
        }

        protected Texture2D bulletTexture;
        protected List<Bullet> bulletList = new List<Bullet>();

        /// <summary>
        /// Get if the tower has a target.
        /// </summary>
        public virtual bool HasTarget
        {
            get
            {
                return target != null;
            }
        }

        #endregion variables

        #region functions

        /// <summary>
        /// Create a new tower
        /// </summary>
        /// <param name="texture">The tower twxture</param>
        /// <param name="bulletTexture">The bullet Texture</param>
        /// <param name="position">The tower position</param>
        public Tower(Texture2D texture, Texture2D bulletTexture, Vector2 position) : base(texture, position)
        {
            this.bulletTexture = bulletTexture;
        }

        /// <summary>
        /// Checks if an enemy is in range of the tower
        /// </summary>
        /// <param name="pos">The position of the enemy</param>
        /// <returns>Returns true if enemy in range</returns>
        public bool IsInRange(Vector2 pos)
        {
            return Vector2.Distance(center, pos) <= radius;
        }

        /// <summary>
        /// Get the closest enemy from a tower
        /// </summary>
        /// <param name="enemies">The list of the _enemies</param>
        public virtual void GetClosestEnemy(List<Enemy> enemies)
        {
            target = null;
            float smallestRange = radius;

            foreach (Enemy enemy in enemies)
            {
                if (Vector2.Distance(center, enemy.Center) < smallestRange)
                {
                    smallestRange = Vector2.Distance(center, enemy.Center);
                    target = enemy;
                }
            }
        }

        /// <summary>
        /// Rotate the tower to the faced enemy
        /// </summary>
        protected void FaceTarget()
        {
            Vector2 direction = center - target.Center;
            direction.Normalize();
            rotation = (float)Math.Atan2(-direction.X, direction.Y);
        }

        /// <summary>
        /// Updates the the tower
        /// </summary>
        /// <param name="gameTime">The current game time</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            bulletTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if(target != null)
            {
                FaceTarget();
                if(!IsInRange(target.Center) || target.IsDead)
                {
                    target = null;
                    bulletTimer = 0;
                }
            }
        }

        /// <summary>
        /// Draws the current tower
        /// </summary>
        /// <param name="spriteBatch">A group of sprite settings</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (Bullet bullet in bulletList)
                bullet.Draw(spriteBatch);

            base.Draw(spriteBatch);
        }

        #endregion functions

        #region events



        #endregion events
    }
}
